import React from 'react';

const questions = {
  choice: [
    {
      answer: ['A','B','C','D','E']
    },
    {
      answer: ['A','B','C','D','E']
    },
    {
      answer: ['A','B','C','D','E']
    },
    {
      answer: ['A','B','C','D','E']
    },
    {
      answer: ['A','B','C','D','E']
    },
    {
      answer: ['A','B','C','D','E']
    },
    {
      answer: ['A','B','C','D','E']
    },
    {
      answer: ['A','B','C','D','E']
    },
    {
      answer: ['A','B','C','D','E']
    }
  ],
  normal: [
    {
      title: '这是题目1',
    },
    {
      title: '这是题目2',
      total: 3
    },
    {
      title: '这是题目3',
    },
    {
      title: '这是题目4',
    },
    {
      title: '这是题目5',
    },
    {
      title: '这是题目6',
    },
    {
      title: '这是题目7',
    },
    {
      title: '这是题目8',
    },
    {
      title: '这是题目9',
    },
    {
      title: '这是题目10',
    },
    {
      title: '这是题目11',
      type: 'synthesize'
    },
    {
      title: '这是题目12',
      type: 'synthesize'
    },
    {
      title: '这是题目13',
      type: 'synthesize',
      total: 2
    }
  ]
}

const a4Size = {
  width: 595,
  height: 842
}

// 标题高度
const titleHeight = 24;
// 内容高度
const contentHeight = 80;
// 底部边距
const bottomHeight = 20;

const copy = (data: any) => JSON.parse(JSON.stringify(data));

class Test extends React.PureComponent {
  state = {
    questions: []
  }

  componentDidMount() {
    this.setState({
      questions: this.resetData(questions.normal)
    });
  }

  resetData = (list: any[]) => {
    const cache: any = {};
    list.forEach((curItem, index: number) => {
      const preItem = list[index - 1];
      const { total = 1 } = curItem;
      // 如果是普通的填空题
      const posY = preItem?.bottomY || 0;

      // 获取当前题目标题的底部Y值
      const titleBottomY = posY + titleHeight;

      // 内容的总高度
      const totalContentHeight = contentHeight * total;

      // 题目的底部Y值
      const bottomY = titleBottomY + totalContentHeight + bottomHeight;

      // 纸的张数
      let paperNum = Math.ceil(posY / a4Size.height) || 1;

      // 当前页的总高度
      const curPageHeight = paperNum  * a4Size.height;

      curItem.posY = posY;
      curItem.bottomY = bottomY;

      // 检查当前页能否放下标题
      if (titleBottomY > curPageHeight) {
        paperNum = paperNum + 1;
        if (!cache[paperNum]) {
          cache[paperNum] = { count: 0, curItem }
        }
        cache[paperNum].count ++;
      } else {
        //如果能够放下，需要检测下面额content能不能放下，通过for循环去检测
        for(let i = 0;i < total;i++) {
          const curY = titleBottomY + (i+1) * contentHeight;
          // 检测到如果题干大于纸的高度，结束
          const paperHeight = paperNum * a4Size.height;
          if (curY > paperHeight) {
            paperNum += 1;
            cache[paperNum] = { count: 1, curItem }
          } else {
            if (!cache[paperNum]) {
              cache[paperNum] = { count: 0, curItem }
            }
            cache[paperNum].count ++;
          }
        }
      }
    });

    console.log(cache);
  }

  render() {
    return (
      <div>
        1111
      </div>
    )
  }
}

export default Test;
